1542C - Strange Function - CodeForces Solution


math number theory *1600

Please click on ads to support us..

Python Code:

import math
n=int(input())
def solve(k):
    i=2
    kq=0
    mod=10**9+7
    lcm=1
    while lcm<=k:
        l=k//lcm
        lcm=(lcm*i)//(math.gcd(lcm, i))
        r=k//lcm
        kq+=(l-r)*i
        i+=1
    return kq%mod
ls=[]
for i in range(n):
    ls.append(int(input()))
for i in ls:
    print(solve(i))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define ull unsigned long long 
#define lld long double
#define lcm(x,y) (x/__gcd(x,y)*y)
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define setbit __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fr(i ,t , n) for (int i = t; i <= n; i++)
#define ceilval(a,b) ((a / b) + ((a % b) != 0));
#define endl "\n"
#define ub upper_bound
#define lb lower_bound
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

const ll M=1e9+7;
ll binexp(ll a,ll b,ll m){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(1LL*ans*a)%m;
        }
        a=(1LL*a*a)%m;
        b=(b>>1);
    }
    return ans;
}

bool ispal(string S){
    for (int i = 0; i < S.length() / 2; i++) {
        if (S[i] != S[S.length() - i - 1]) return 0;
    }
    return 1;
}

bool isPrime(ll n){
    if (n <= 1) return 0;
    if (n == 2 || n == 3) return 1;
    if (n % 2 == 0 || n % 3 == 0) return 0;
    for (ll i = 5; i <= sqrt(n); i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return 0;
     return 1;
}

vector<int> pfsq(int n){
    vector<int>ans;
    for(int i=2 ; i*i<=n; ++i){
        while(n%i==0){
            ans.pb(i);
            n/=i;
        }
    }
    if(n>1) ans.pb(n);
    return ans;
}

//---------------------------------------------------------------------

// const int NN=1e7+10;
// ll fact[NN];

// void initfact(){
//     fact[0]=1;
//     for (int i = 1; i < NN; ++i){
//         fact[i]=(i*fact[i-1]*1LL)%M;
//     }
// }

// ll nck(ll n,ll k){
//     ll ans=fact[n];
//     ll deno=(fact[k]*fact[n-k]*1LL)%M;
//     ans=(ans*binexp(deno,M-2,M)*1LL)%M;
//     return ans;
// }

//-------------------

// const int NN=1e7+10;
// vector<int>b(NN,1);
// // vector<int>hp(NN,0);

// void sieve(){
//     b[0]=b[1]=0;
//     for(int i=2; i<NN; i++){
//         if(b[i]){
//             // hp[i]=i;
//             for(int j=2*i; j<NN; j+=i){
//                 b[j]=0;
//                // hp[j]=i;
//             }
//         }
//     }
// }

// vector<int> pfsieve(int n){
//     vector<int>ans;
//     while(n>1){
//         int p=hp[n];
//         while(n%p==0){
//             n=n/p;
//             ans.pb(p);
//         }
//     }
//     return ans;
// }

//---------------------------------------------------------------------



void solve(){
    ll tt=1; 
    cin>>tt; 
    while(tt--){
        ll n; cin>>n;
        ll ans=0,l=1,i=1;
        while(l<=n){
            ans+=(n/l);
            l=lcm(l,i);
            i++;
        }        
        cout<<ans%M<<endl;
    }
}


//---------------------------------------------------------------------
int main() {
    #ifndef ONLINE_JUDGE
      freopen("dbg.txt","w",stderr);
    #endif
    fastio();

    solve();
}


Comments

Submit
0 Comments
More Questions

938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths